package com.alpha.processor.processor.anno_processor;

import com.alpha.binder.anno.motion.OnViewClick;
import com.alpha.processor.creators.connector_file.ConnectorFileCreator;
import com.alpha.processor.processor.tools.ProcessorTool;
import com.alpha.processor.processor.anno_processor.base.AnnoProcessor;

import java.util.Map;
import java.util.Set;

import javax.annotation.processing.Messager;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

public class OnViewClickAnnoProcessor extends AnnoProcessor {

    private final Map<String, ConnectorFileCreator> connectorCreatorMap;

    public OnViewClickAnnoProcessor(Messager messager, Elements elementUtils, ProcessorTool logger, Map<String, ConnectorFileCreator> connectorCreatorMap) {
        super(messager, elementUtils, logger);
        this.connectorCreatorMap = connectorCreatorMap;
    }

    @Override
    public void processAnno(RoundEnvironment roundEnvironment) {
        Set<? extends Element> elementSet = roundEnvironment.getElementsAnnotatedWith(OnViewClick.class);
        for (Element element : elementSet) {
            TypeElement classElement = (TypeElement) element.getEnclosingElement();
            String fullClassName = classElement.getQualifiedName().toString();
            ConnectorFileCreator fileCreator = connectorCreatorMap.get(fullClassName);
            if (fileCreator == null) {
                fileCreator = new ConnectorFileCreator(logger, elementUtils, classElement);
                connectorCreatorMap.put(fullClassName, fileCreator);
            }
            String methodName = element.getSimpleName().toString();
            fileCreator.getCreateModel().getViewInfoModel().putClickMethodElement(methodName, element);
        }
    }
}
